package main.database;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import main.GlobalConfig;
import org.bson.Document;

import java.util.ArrayList;
import java.util.List;

public class DiscussionHelper {
    public static List<List<String>> getDiscussionOfArticle(String articleId, String currentUUID) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfCommentToArticle);
        FindIterable<Document> findIterable = collection.find(Filters.eq("fictionid", articleId));
        MongoCursor<Document> cursor = findIterable.iterator();

        List<List<String>> resultList = new ArrayList<>();
        while (cursor.hasNext()) {
            Document doc = cursor.next();

            List<String> list = new ArrayList<>();
            String uuid = (String) doc.get("userid");
            String account = AccountHelper.getAccountInfoFromUUID(uuid);
            String commentId = doc.get("_id").toString();
            long thumbupCount = CommentThumbHelper.getCommentThumbupCount(commentId);
            long thumbdownCount = CommentThumbHelper.getCommentThumbdownCount(commentId);

            list.add(account);                         // comment creator's name
            list.add(uuid);                            // comment creator's uuid
            list.add((String) doc.get("createtime"));  // comment create time
            list.add((String) doc.get("comment"));     // comment content
            list.add(commentId);                       // comment id
            list.add(String.valueOf(thumbupCount));    // thumbup count
            list.add(String.valueOf(thumbdownCount));  // thumbdown count
            String gesture = CommentThumbHelper.getCommentGestureFromUUID(commentId, currentUUID);
            list.add(gesture);

            resultList.add(list);
        }

        cursor.close();

        return resultList;
    }

    public static List<String[]> getUserDiscussionInfoList(String uuid) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfCommentToArticle);
        FindIterable<Document> findIterable = collection.find(Filters.eq("userid", uuid));
        MongoCursor<Document> cursor = findIterable.iterator();

        List<String[]> resultList = new ArrayList<>();
        while(cursor.hasNext()) {
            // article (id, name), time, comment
            Document doc = cursor.next();

            String commentId = doc.get("_id").toString();
            String[] result = new String[6];
            result[0] = doc.get("createtime").toString(); //time
            result[1] = doc.get("comment").toString();    //content

            String fictionId = doc.get("fictionid").toString();
            result[2] = fictionId;                        //article id
            String[] articleInfos = ArticleHelper.getFieldsFromId2(fictionId, true);

            result[3] = articleInfos[0];                  // article title

            long thumbupCount = CommentThumbHelper.getCommentThumbupCount(commentId);
            long thumbdownCount = CommentThumbHelper.getCommentThumbdownCount(commentId);
            result[4] = String.valueOf(thumbupCount);     // thumb up count
            result[5] = String.valueOf(thumbdownCount);   // thumb down count

            resultList.add(result);
        }
        cursor.close();

        return resultList;
    }
}
